#include <stdint.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>

size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    printf("%s\n",result);
    //解析为json格式的字符串
    cJSON *root=cJSON_Parse(result);
    //做非空判断
    if(root!=NULL)
    {
        cJSON *successRoot=cJSON_GetObjectItemCaseSensitive(root,"success");
        cJSON *codeRoot=cJSON_GetObjectItemCaseSensitive(root,"code");
        cJSON *messageRoot=cJSON_GetObjectItemCaseSensitive(root,"message");
        if(cJSON_IsTrue(successRoot))
         {
            printf("ttt");
            printf("成功！%s",messageRoot->valuestring);
         }else{
             printf("失败！！%s",messageRoot->valuestring);
             printf("fffff");
         }
    }
    return realsize;
}
int main(int argc, char const *argv[])
{
    //初始化
    CURL *curl = curl_easy_init();
    if (curl)
    {
        //设置相关参数  url  post数据 设置响应回调
         curl_easy_setopt(curl,CURLOPT_URL,
                 "http://47.109.65.208/config.json");
        //设置回调函数         
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);
        //发请求
         CURLcode res = curl_easy_perform(curl);
        //判断是否发送成功
        if (res!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        //清理
        curl_easy_cleanup(curl);
    }
    
    return 0;
}
